package com.loveyukari.controller;

import com.loveyukari.common.Pager;
import com.loveyukari.model.BanAuth;
import com.loveyukari.service.AuthService;
import com.loveyukari.service.BanService;
import com.loveyukari.service.ManagerService;
import com.loveyukari.support.JsonResult;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin/ban")
public class BanController {


    @Autowired
    private BanService banService;
    @Autowired
    private ManagerService managerService;


    @RequestMapping("/byLiwu")
    @ResponseBody
    public JsonResult banByLiwu(int authId, HttpServletRequest request){
        try {
            String sign =(String) request.getAttribute("sign");
            int managerId = managerService.getManageAuthId(sign);
            banService.banAccount(authId,managerId,43200,"礼物页面封禁");
            return new JsonResult(0,"封禁成功");
        } catch (Exception e) {
            return new JsonResult(1,"封禁失败: "+e.getMessage());
        }
    }

    @RequestMapping("/byAdmin")
    @ResponseBody
    public JsonResult banByAdmin(String name,int time,String message,HttpServletRequest request){
        try {
            String sign =(String) request.getAttribute("sign");
            int managerId = managerService.getManageAuthId(sign);
            banService.banAccount(name,managerId,time,message);
            return new JsonResult(0,"封禁成功");
        } catch (Exception e) {
            return new JsonResult(1,"封禁失败: "+e.getMessage());
        }
    }

    @RequestMapping("/removeAccount")
    @ResponseBody
    public JsonResult removeAccount(int authId){
        try {
            banService.removeAccount(authId);
            return new JsonResult(0,"解禁成功");
        } catch (Exception e) {
            return new JsonResult(1,"解禁失败: "+e.getMessage());
        }


    }

    @RequestMapping("/updateAutoShutUp")
    @ResponseBody
    public JsonResult updateAutoShutUp(int id,HttpServletRequest request){
        try {
            String sign =(String) request.getAttribute("sign");
            int managerId = managerService.getManageAuthId(sign);
            banService.updateAccountAutoShutUp(id,managerId);
            return new JsonResult(0,"修改成功");
        } catch (Exception e) {
            return new JsonResult(1,"修改失败: "+e.getMessage());
        }
    }

    @RequestMapping("/index")
    public String index(Model model,Pager pager){
        List<BanAuth> list = banService.getBanAuthList(pager);
        model.addAttribute("list",list);
        model.addAttribute("pager",pager);
        return "ban/index";
    }



}
